/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 上午11:08:08
 * HomeController.java
 * 
 */
package com.fujitsu.nanjing.mobile.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.mobile.base.BaseController;
import com.fujitsu.nanjing.mobile.common.Param;
import com.fujitsu.nanjing.mobile.common.ReflectUtils;
import com.fujitsu.nanjing.mobile.exception.CompanyNotFoundException;
import com.fujitsu.nanjing.mobile.exception.NotOneException;
import com.fujitsu.nanjing.mobile.exception.Resource404Exception;
import com.fujitsu.nanjing.mobile.model.hibernate.CoreCompany;
import com.fujitsu.nanjing.mobile.model.hibernate.CoreUser;
import com.fujitsu.nanjing.mobile.service.CoreCompanyService;
import com.fujitsu.nanjing.mobile.service.CoreUserService;

/**
 * @author qixb.fnst
 * 
 */
@Controller
public class HomeController extends BaseController {
	@Resource
	private CoreUserService coreUserService;

	@Resource
	private CoreCompanyService coreCompanyService;

	private static List<String> items = null;
	public static final String HOME = "home";
	public static final String MAP = "map";
	public static final String _MAP = "_map";
	public static final String ATTENDENCE = "attendence";
	public static final String NOTIFICATION = "notification";
	public static final String USER = "user";
	public static final String TASK = "task";
	public static final String COMPANY = "company";
	public static final String PROFILE = "profile";
	public static final String TRIGGER = "trigger";

	static {
		items = new ArrayList<String>();
		items.add(HOME);
		items.add(MAP);
		items.add(_MAP);
		items.add(ATTENDENCE);
		items.add(NOTIFICATION);
		items.add(USER);
		items.add(TASK);
		items.add(COMPANY);
		items.add(TRIGGER);
		items.add(PROFILE);
	}

	@RequestMapping(value = {"/login", "login.html"}, method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = {"/{type}"}, method = RequestMethod.GET)
	public String getPage(@PathVariable String type, Model model,
			HttpSession session) throws Resource404Exception,
			CompanyNotFoundException {
		String typeInVelocity = type;
		if (type != null) {
			typeInVelocity = typeInVelocity.toLowerCase().trim();
			if (items.contains(type)) {
				model.addAttribute("type", typeInVelocity);
				handleType(type, model, session);
				return type;
			}
			throw new Resource404Exception(type);
		}
		throw new Resource404Exception(type);
	}

	@RequestMapping(value = {"/login"}, method = RequestMethod.POST)
	public String login(String company, String username, String password,
			String remember, Model model, HttpSession session) {
		CoreUser user = coreUserService.checkUser(company, username, password);
		if (user == null) {
			model.addAttribute("error", "用户名或者密码错误！");
			return "login";
		} else {
			session.setAttribute(SESSION_USER, user);
			return redirect("/index");
		}
	}

	@RequestMapping(value = {"/index", "index.html", "/", "/home"}, method = RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute("type", "home");
		return "index";
	}

	private void handleType(String type, Model model, HttpSession session)
			throws CompanyNotFoundException {
		if (type.equals(HOME)) {

		} else if (type.equals(MAP)) {

		} else if (type.equals(_MAP)) {
			CoreUser coreUser = getSessionUser(session);
			if (coreUser == null) {
				redirect("/login");
			}
			CoreCompany coreCompany = null;
			try {
				coreCompany = coreCompanyService.getCompanyByCompanyId(coreUser
						.getCompanyId());
			} catch (NotOneException e) {
				throw new CompanyNotFoundException("<font color='red'>"
						+ coreUser.getCompanyName() + "</font>");
			}
			model.addAttribute("company", coreCompany);

		} else if (type.equals(ATTENDENCE)) {

		} else if (type.equals(NOTIFICATION)) {

		} else if (type.equals(USER)) {
			CoreUser coreUser = getSessionUser(session);
			if (coreUser == null) {
				redirect("/login");
			}
			String whereClause = " companyId=" + coreUser.getCompanyId();
			int companyNums = coreUserService.getUserNums(whereClause);
			List<CoreUser> users = coreUserService.getUsers(null, whereClause,
					false, 0, 10);
			model.addAttribute("nums", companyNums);
			model.addAttribute("users", users);
		} else if (type.equals(TASK)) {

		} else if (type.equals(COMPANY)) {
			int companyNums = coreCompanyService.getCompanyNums(null);
			List<CoreCompany> companies = coreCompanyService.getCompanies(null,
					null, false, 0, 10);
			model.addAttribute("nums", companyNums);
			model.addAttribute("companies", companies);
		} else if (type.equals(TRIGGER)) {

		} else if (type.equals(PROFILE)) {
			CoreUser coreUser = getSessionUser(session);
			if (coreUser == null) {
				redirect("/login");
			}
			coreUser = coreUserService.find(coreUser.getId());
			List<String> unEdit = new ArrayList<String>();
			unEdit.add("serialVersionUID");
			unEdit.add("id");
			unEdit.add("password");
			unEdit.add("companyId");
			unEdit.add("username");
			unEdit.add("companyId");
			unEdit.add("companyAdmin");
			unEdit.add("companyName");
			unEdit.add("state");
			unEdit.add("opuserId");
			unEdit.add("roleId");
			unEdit.add("createDatetime");
			unEdit.add("updateDatetime");
			List<Param> params = ReflectUtils.initParams(CoreUser.class,
					coreUser, unEdit);
			model.addAttribute("params", params);
		} else {

		}
	}

}
